package com.ccl.iot.views;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.ccl.iot.R;
import com.ccl.iot.asynctask.SendIrTask;
import com.ccl.iot.bean.CodeviewBean;
import com.ccl.iot.device.IOTSubBLir;
import com.ccl.iot.dvr.utils.GlideUtil;
import com.ccl.iot.ir.BlirConst;
import com.ccl.iot.ir.DBUtils;
import com.ccl.iot.publics.Messager;
import com.hzy.tvmao.KKACManagerV2;
import com.hzy.tvmao.ir.ac.ACConstants;
import com.hzy.tvmao.ir.ac.ACStateV2;
import com.hzy.tvmao.utils.DataStoreUtil;

import java.util.Arrays;
import java.util.HashMap;

public class IrAirControlView extends LinearLayout {

    private ImageView mAirTemp16Img, mAirTemp17Img, mAirTemp18Img, mAirTemp19Img, mAirTemp20Img, mAirTemp21Img,
            mAirTemp22Img, mAirTemp23Img, mAirTemp24Img, mAirTemp25Img, mAirTemp26Img, mAirTemp27Img, mAirTemp28Img,
            mAirTemp29Img, mAirTemp30Img, mAirTemp31Img, mAirTemp32Img;

    private SeekBar mAirSeekBar;
    private ImageView mAirModeImg;
    private TextView mAirModeName;
    private ImageView mAirSpeedImg;
    private TextView mAirSpeedName;
    private ImageView mAirDirImg;
    private TextView mAirDirName;
    private LinearLayout mAirSwitchLayout;

    /**
     * 控制器
     */
    private KKACManagerV2 mKKACManager;

    private PopupWindow mAirSpeedPop;
    private View mAirSpeedPopView;
    private LinearLayout mAirModeWetLayout;
    private LinearLayout mAirModeWindLayout;
    private LinearLayout mAirModeHotLayout;
    private LinearLayout mAirModeCoolLayout;
    private LinearLayout mAirModeAutoLayout;
    private LinearLayout mAirSpeedShowLayout;
    private LinearLayout mAirSpeedMiddleLayout;
    private LinearLayout mAirSpeedFastLayout;

    private ImageView mAirSWitchImg;
    private View mAirModePopView;
    private PopupWindow mAirModePop;
    private LinearLayout mAirModeLayout;
    private LinearLayout mAirSpeedLayout;
    private LinearLayout mAirDirLayout;
    private LinearLayout mAirBtnListLayout;

    private IOTSubBLir mSubBLir;
    private SubBlirControlView.OnDevOnlineChangeListener mOnlineChangeListener;
    private View mOfflineMask;
    private ImageView mTypeView;
    private boolean mCheckable;
    private Context mContext;

    public IrAirControlView(Context context) {
        this(context, null);
    }

    public IrAirControlView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IrAirControlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
        initData();
        initListener();
    }

    public void initView() {
        View.inflate(getContext(), R.layout.view_air_control_part, this);
        mAirTemp16Img = (ImageView) findViewById(R.id.iv_air_16);
        mAirTemp17Img = (ImageView) findViewById(R.id.iv_air_17);
        mAirTemp18Img = (ImageView) findViewById(R.id.iv_air_18);
        mAirTemp19Img = (ImageView) findViewById(R.id.iv_air_19);
        mAirTemp20Img = (ImageView) findViewById(R.id.iv_air_20);
        mAirTemp21Img = (ImageView) findViewById(R.id.iv_air_21);
        mAirTemp22Img = (ImageView) findViewById(R.id.iv_air_22);
        mAirTemp23Img = (ImageView) findViewById(R.id.iv_air_23);
        mAirTemp24Img = (ImageView) findViewById(R.id.iv_air_24);
        mAirTemp25Img = (ImageView) findViewById(R.id.iv_air_25);
        mAirTemp26Img = (ImageView) findViewById(R.id.iv_air_26);
        mAirTemp27Img = (ImageView) findViewById(R.id.iv_air_27);
        mAirTemp28Img = (ImageView) findViewById(R.id.iv_air_28);

        mAirTemp29Img = (ImageView) findViewById(R.id.iv_air_29);
        mAirTemp30Img = (ImageView) findViewById(R.id.iv_air_30);
        mAirTemp31Img = (ImageView) findViewById(R.id.iv_air_31);
        mAirTemp32Img = (ImageView) findViewById(R.id.iv_air_32);

        mAirSeekBar = (SeekBar) findViewById(R.id.seekBar_air);
        mAirSeekBar.setMax(160);

        mAirModeLayout = (LinearLayout) findViewById(R.id.layout_airmode);
        mAirSpeedLayout = (LinearLayout) findViewById(R.id.layout_airspeed);
        mAirDirLayout = (LinearLayout) findViewById(R.id.layout_airdir);
        mAirSwitchLayout = (LinearLayout) findViewById(R.id.layout_air_switch);

        mAirBtnListLayout = (LinearLayout) findViewById(R.id.layout_airbtn_list);

        mAirModeImg = (ImageView) findViewById(R.id.iv_airmode);
        mAirModeName = (TextView) findViewById(R.id.tv_airmode_name);

        mAirSpeedImg = (ImageView) findViewById(R.id.iv_airspeed);
        mAirSpeedName = (TextView) findViewById(R.id.tv_airspeed_name);

        mAirDirImg = (ImageView) findViewById(R.id.iv_airdir);
        mAirDirName = (TextView) findViewById(R.id.tv_airdir_name);

        mAirSWitchImg = (ImageView) findViewById(R.id.iv_air_switch);
    }

    public void initData() {
        mKKACManager = new KKACManagerV2();
    }

    public void initListener() {
        mAirTemp16Img.setOnClickListener(mTempListener);
        mAirTemp17Img.setOnClickListener(mTempListener);
        mAirTemp18Img.setOnClickListener(mTempListener);
        mAirTemp19Img.setOnClickListener(mTempListener);
        mAirTemp20Img.setOnClickListener(mTempListener);
        mAirTemp21Img.setOnClickListener(mTempListener);
        mAirTemp22Img.setOnClickListener(mTempListener);
        mAirTemp23Img.setOnClickListener(mTempListener);
        mAirTemp24Img.setOnClickListener(mTempListener);
        mAirTemp25Img.setOnClickListener(mTempListener);
        mAirTemp26Img.setOnClickListener(mTempListener);
        mAirTemp27Img.setOnClickListener(mTempListener);
        mAirTemp28Img.setOnClickListener(mTempListener);

        mAirTemp29Img.setOnClickListener(mTempListener);
        mAirTemp30Img.setOnClickListener(mTempListener);
        mAirTemp31Img.setOnClickListener(mTempListener);
        mAirTemp32Img.setOnClickListener(mTempListener);

        mAirModeLayout.setOnClickListener(mListener);
        mAirSpeedLayout.setOnClickListener(mListener);
        mAirDirLayout.setOnClickListener(mListener);
        mAirSwitchLayout.setOnClickListener(mListener);


        mAirSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (mKKACManager.getPowerState() == ACConstants.AC_POWER_OFF) {
                    Messager.Show("关机下不能使用");
                    return;
                }
                //停的时候才改变温度
                int temp = seekbarValue2Temp(seekBar.getProgress());
                mKKACManager.setTargetTemp(temp);
                mAirSWitchImg.setSelected(mKKACManager.getPowerState() == ACConstants.AC_POWER_ON);
                sendIr();
            }
        });
    }

    public void setSubDev(IOTSubBLir subDev, SubBlirControlView.OnDevOnlineChangeListener l, View maskView, ImageView typeView, boolean checkable) {
        mOnlineChangeListener = l;
        mOfflineMask = maskView;
        mSubBLir = subDev;
        mTypeView = typeView;
        mCheckable = checkable;
        if (mSubBLir != null) {
            HashMap<String, CodeviewBean> irCode = mSubBLir.getIRCode();
            CodeviewBean api = irCode.get(DBUtils.CODE_TYPE_API);
            if (api != null && api.ac != null) {
                mKKACManager.initIRData(api.ac.rid, api.ac.exts, null);//根据空外数据初始化空调解码器
                String acState = DataStoreUtil.i().getString(mSubBLir.getUDID(), "");//获取以前保存过的空调状态
                mKKACManager.setACStateV2FromString(acState);
                if (mCheckable && mSubBLir.getBatchCodes() != null) {
                    CodeviewBean batchCodes = mSubBLir.getBatchCodes();
                    if (batchCodes != null) {
                        String code = batchCodes.aCode;
                        if (code != null) {
                            String stateCode = code.substring(0, 40);
                            int power = Integer.parseInt(stateCode.substring(8, 16), 16);
                            Log.e("setSubDev", "power: " + power);
                            if (mKKACManager.getPowerState() != power) {
                                mKKACManager.changePowerState();
                            }
                            int mode = Integer.parseInt(stateCode.substring(0, 8), 16);
                            Log.e("setSubDev", "mode: " + mode);
                            mKKACManager.changeACTargetModel(mode);

                            int tmp = Integer.parseInt(stateCode.substring(16, 24), 16);
                            Log.e("setSubDev", "tmp: " + tmp);
                            mKKACManager.setTargetTemp(tmp);

                            int speed = Integer.parseInt(stateCode.substring(24, 32), 16);
                            Log.e("setSubDev", "speed: " + speed);
                            mKKACManager.setTargetWindSpeed(speed);

                            int dir = Integer.parseInt(stateCode.substring(32, 40), 16);
                            Log.e("setSubDev", "dir: " + dir);
                            mKKACManager.setTargetUDWindDirect(dir);
                        }
                    }
                }
                updateACDisplay();//刷新空调面板
            }
        }
    }

    private void updateACDisplay() {
        if (mSubBLir != null){
            if (mSubBLir.getImageCount() == 0){
                if (mKKACManager.getPowerState() == ACConstants.AC_POWER_ON) {
                    mAirSWitchImg.setSelected(true);
//                    mTypeView.setImageResource(R.drawable.icon_air_open);
                    GlideUtil.loadImageView(mContext,R.drawable.icon_air_open,mTypeView);
                } else {
                    mAirSWitchImg.setSelected(false);
//                    mTypeView.setImageResource(R.drawable.icon_air_close);
                    GlideUtil.loadImageView(mContext,R.drawable.icon_air_close,mTypeView);
                }
            }else {
                String imagePath = mSubBLir.getImagePath();
                GlideUtil.loadImageViewNoCacheDisk(mContext,imagePath,mTypeView);
            }
        }

        //更新设置温度
        byte setTemp = (byte) mKKACManager.getCurTemp();
        mAirSeekBar.setProgress(temp2SeekbarValue(setTemp));
        updateAirModeImgAndName(getModelValueByKk(mKKACManager.getCurModelType()));
        updateAirSpeedImgAndName((byte) mKKACManager.getCurWindSpeed());
        if (mKKACManager.getCurUDDirect() == 0) {//风向为扫风
            mAirDirName.setText(R.string.door_open);
        } else {
            mAirDirName.setText(R.string.door_close);
        }
    }

    /**
     * 温度值转seekbar进度值
     *
     * @param temp
     * @return
     */
    private int temp2SeekbarValue(int temp) {
        if (temp >= 16 && temp <= 32) {
            return (temp - 16) * 10;
        }
        return 0;
    }

    private byte getModelValueByKk(int kkModel) {
        switch (kkModel) {
            case ACConstants.AC_MODE_DRY:
                return 5;
            case ACConstants.AC_MODE_FAN:
                return 2;
            case ACConstants.AC_MODE_HEAT:
                return 4;
            case ACConstants.AC_MODE_COOL:
                return 3;
            case ACConstants.AC_MODE_AUTO:
                return 1;
        }
        return 1;
    }

    private View.OnClickListener mTempListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int seebBarProgress = 0;
            switch (v.getId()) {
                case R.id.iv_air_16:
                    seebBarProgress = 0;
                    break;
                case R.id.iv_air_17:
                    seebBarProgress = 10;
                    break;
                case R.id.iv_air_18:
                    seebBarProgress = 20;
                    break;
                case R.id.iv_air_19:
                    seebBarProgress = 30;
                    break;
                case R.id.iv_air_20:
                    seebBarProgress = 40;
                    break;
                case R.id.iv_air_21:
                    seebBarProgress = 50;
                    break;
                case R.id.iv_air_22:
                    seebBarProgress = 60;
                    break;
                case R.id.iv_air_23:
                    seebBarProgress = 70;
                    break;
                case R.id.iv_air_24:
                    seebBarProgress = 80;
                    break;
                case R.id.iv_air_25:
                    seebBarProgress = 90;
                    break;
                case R.id.iv_air_26:
                    seebBarProgress = 100;
                    break;
                case R.id.iv_air_27:
                    seebBarProgress = 110;
                    break;
                case R.id.iv_air_28:
                    seebBarProgress = 120;
                    break;
                case R.id.iv_air_29:
                    seebBarProgress = 130;
                    break;
                case R.id.iv_air_30:
                    seebBarProgress = 140;
                    break;
                case R.id.iv_air_31:
                    seebBarProgress = 150;
                    break;
                case R.id.iv_air_32:
                    seebBarProgress = 160;
                    break;
                default:
                    break;
            }

            mAirSeekBar.setProgress(seebBarProgress);

            int temp = seekbarValue2Temp(seebBarProgress);

            if (mKKACManager.getPowerState() == ACConstants.AC_POWER_OFF) {
                Messager.Show("关机下不能使用");
                return;
            }
            if (!mKKACManager.isTempCanControl()) {//该模式下是否支持调节温度
                Messager.Show("在该模式下温度不能调节");
                return;
            }
            mKKACManager.setTargetTemp(temp);
            sendIr();
        }
    };

    private View.OnClickListener mListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            switch (v.getId()) {

                //底部4个按钮的点击事件
                case R.id.layout_airmode:
                    showAirModePop();
                    break;
                case R.id.layout_airspeed:
                    showAirSpeedPop();
                    break;
                case R.id.layout_airdir:
                    if (mKKACManager.getPowerState() == ACConstants.AC_POWER_OFF) {
                        Messager.Show("关机下不能使用");
                        return;
                    }

                    if (mKKACManager.getCurUDDirectType() == ACStateV2.UDWindDirectType.UDDIRECT_ONLY_SWING) {//没有风向，风向不能使用
                        Messager.Show("没有风向可调节");
                        return;
                    }

                    if (mKKACManager.getCurUDDirectType() == ACStateV2.UDWindDirectType.UDDIRECT_ONLY_FIX) {//只有固定风向可用，扫风不能使用
                        Messager.Show("扫风键不可用");
                        return;
                    }
                    mKKACManager.changeUDWindDirect(ACStateV2.UDWindDirectKey.UDDIRECT_KEY_SWING);//切换风向，对于同时具备扫风和固定的空调：如果当前是扫风，再摁扫风时，切换到了固定风向；如果当前是固定风向，再摁扫风时，切换到扫风
                    if (mKKACManager.getCurUDDirect() == 0) {//风向为扫风
                        mAirDirName.setText(R.string.door_open);
                    } else {
                        mAirDirName.setText(R.string.door_close);
                    }
                    sendIr();
                    break;
                case R.id.layout_air_switch:
                    //空调开关
                    mKKACManager.changePowerState();//切换开关机
                    updateACDisplay();
                    sendIr();
                    break;

                default:
                    break;
            }
        }
    };

    private View.OnClickListener mModeListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mKKACManager.getPowerState() == ACConstants.AC_POWER_OFF) {
                Messager.Show("关机下不能使用");
                return;
            }
            switch (v.getId()) {

                //模式pop弹出来的5个按钮
                case R.id.layout_airmode_wet:       //除湿
                    if (!mKKACManager.isContainsTargetModel(ACConstants.AC_MODE_DRY)) {//是否支持模式
                        Messager.Show("该空调不具备除湿模式");
                        return;
                    }
                    mKKACManager.changeACTargetModel(ACConstants.AC_MODE_DRY);
                    updateAirModeImgAndName((byte) 5);
                    break;
                case R.id.layout_airmode_wind:      //送风

                    if (!mKKACManager.isContainsTargetModel(ACConstants.AC_MODE_FAN)) {//是否支持模式
                        Messager.Show("该空调不具备送风模式");
                        return;
                    }
                    mKKACManager.changeACTargetModel(ACConstants.AC_MODE_FAN);
                    updateAirModeImgAndName((byte) 2);
                    break;
                case R.id.layout_airmode_hot:       //制热
                    if (!mKKACManager.isContainsTargetModel(ACConstants.AC_MODE_HEAT)) {//是否支持模式
                        Messager.Show("该空调不具备制热模式");
                        return;
                    }
                    mKKACManager.changeACTargetModel(ACConstants.AC_MODE_HEAT);
                    updateAirModeImgAndName((byte) 4);
                    break;
                case R.id.layout_airmode_cool:      //制冷

                    if (!mKKACManager.isContainsTargetModel(ACConstants.AC_MODE_COOL)) {//是否支持模式
                        Messager.Show("该空调不具备制冷模式");
                        return;
                    }
                    mKKACManager.changeACTargetModel(ACConstants.AC_MODE_COOL);
                    updateAirModeImgAndName((byte) 3);
                    break;
                case R.id.layout_airmode_auto:      //自动

                    if (!mKKACManager.isContainsTargetModel(ACConstants.AC_MODE_AUTO)) {//是否支持模式
                        Messager.Show("该空调不具备自动模式");
                        return;
                    }
                    mKKACManager.changeACTargetModel(ACConstants.AC_MODE_AUTO);
                    updateAirModeImgAndName((byte) 1);
                    break;
                default:
                    break;
            }

            mAirModePop.dismiss();
            sendIr();
        }
    };

    private View.OnClickListener mSpeedListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mKKACManager.getPowerState() == ACConstants.AC_POWER_OFF) {
                Messager.Show("关机下不能使用");
                return;
            }
            if (!mKKACManager.isWindSpeedCanControl()) {//该模式下是否支持调节风速
                Messager.Show("该模式下风速不能调节");
                return;
            }
            switch (v.getId()) {

                //风速pop弹出来的3个按钮
                case R.id.layout_airspeed_slow:
                    mKKACManager.setTargetWindSpeed(ACConstants.AC_WIND_SPEED_LOW);//切换风速档
                    updateAirSpeedImgAndName(ACConstants.AC_WIND_SPEED_LOW);
                    break;
                case R.id.layout_airspeed_middle:
                    mKKACManager.setTargetWindSpeed(ACConstants.AC_WIND_SPEED_MEDIUM);//切换风速档
                    updateAirSpeedImgAndName(ACConstants.AC_WIND_SPEED_MEDIUM);
                    break;
                case R.id.layout_airspeed_fast:
                    mKKACManager.setTargetWindSpeed(ACConstants.AC_WIND_SPEED_HIGH);//切换风速档
                    updateAirSpeedImgAndName(ACConstants.AC_WIND_SPEED_HIGH);
                    break;
                default:
                    break;
            }

            mAirSpeedPop.dismiss();
            sendIr();
        }
    };

    /**
     * 更新模式图片和名字
     *
     * @param mode
     * @return
     */
    private void updateAirModeImgAndName(byte mode) {
        int imgId = R.drawable.icon_airmode_auto;
        int txtId = R.string.auto;
        switch (mode) {
            case 1:
                imgId = R.drawable.icon_airmode_auto;
                txtId = R.string.auto;
                break;
            case 2:
                imgId = R.drawable.icon_airmode_wind;
                txtId = R.string.airmode_wind;
                break;
            case 3:
                imgId = R.drawable.icon_airmode_cool;
                txtId = R.string.airmode_cool;
                break;
            case 4:
                imgId = R.drawable.icon_airmode_hot;
                txtId = R.string.airmode_hot;
                break;
            case 5:
                imgId = R.drawable.icon_airmode_wet;
                txtId = R.string.airmode_wet;
                break;
            default:
                imgId = R.drawable.icon_unknown;
                txtId = R.string.Unknown;
                break;
        }
        mAirModeImg.setImageResource(imgId);
        mAirModeName.setText(getResources().getString(txtId));
    }

    /**
     * 更新风速图片和名字
     *
     * @param speed
     */
    private void updateAirSpeedImgAndName(byte speed) {
        int imgId = R.drawable.icon_airspeed_slow;
        int txtId = R.string.airspeed_slow;
        switch (speed) {

            //风速pop弹出来的3个按钮
            case 1:
                imgId = R.drawable.icon_airspeed_slow;
                txtId = R.string.airspeed_slow;
                break;
            case 2:
                imgId = R.drawable.icon_airspeed_middle;
                txtId = R.string.airspeed_middle;
                break;
            case 3:
                imgId = R.drawable.icon_airspeed_fast;
                txtId = R.string.airspeed_fast;
                break;
            default:
                break;
        }
        mAirSpeedImg.setImageResource(imgId);
        mAirSpeedName.setText(getResources().getString(txtId));
    }

    /**
     * 将进度值转成温度
     *
     * @param progress
     */
    private int seekbarValue2Temp(int progress) {
        return progress / 10 + 16;
    }

    /*
     *弹出模式PopupWindow
    */
    private void showAirModePop() {
        Log.e("modepop", "showAirModePop");
        if (mAirModePopView == null) {
            mAirModePopView = inflate(getContext(), R.layout.pop_air_mode, null);
            mAirModeWetLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_wet);
            mAirModeWindLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_wind);
            mAirModeHotLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_hot);
            mAirModeCoolLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_cool);
            mAirModeAutoLayout = (LinearLayout) mAirModePopView.findViewById(R.id.layout_airmode_auto);

            mAirModeWetLayout.setOnClickListener(mModeListener);
            mAirModeWindLayout.setOnClickListener(mModeListener);
            mAirModeHotLayout.setOnClickListener(mModeListener);
            mAirModeCoolLayout.setOnClickListener(mModeListener);
            mAirModeAutoLayout.setOnClickListener(mModeListener);
        }

        if (mAirModePop == null) {
            mAirModePop = new PopupWindow(mAirModePopView, mAirModeLayout.getWidth(), 704);
            // 设置动画
            //            mAirModePop.setAnimationStyle(R.style.take_photo_anim);
            //  设置背景颜色
            mAirModePop.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
            // 设置可以获取焦点
            mAirModePop.setFocusable(true);
            // 设置可以触摸弹出框以外的区域
            mAirModePop.setOutsideTouchable(true);

        }

        if (mAirModePop.isShowing()) {
            mAirModePop.dismiss();
        } else {
            //  以下拉的方式显示，并且可以设置显示的位置
            mAirModePop.showAsDropDown(mAirBtnListLayout, 0, 0);
        }

    }

    /**
     * 弹出风速PopupWindow
     */
    private void showAirSpeedPop() {
        Log.e("SpeedPop", "showAirSpeedPop");
        if (mAirSpeedPopView == null) {
            mAirSpeedPopView = inflate(getContext(), R.layout.pop_air_speed, null);

            mAirSpeedShowLayout = (LinearLayout) mAirSpeedPopView.findViewById(R.id.layout_airspeed_slow);
            mAirSpeedMiddleLayout = (LinearLayout) mAirSpeedPopView.findViewById(R.id.layout_airspeed_middle);
            mAirSpeedFastLayout = (LinearLayout) mAirSpeedPopView.findViewById(R.id.layout_airspeed_fast);

            mAirSpeedShowLayout.setOnClickListener(mSpeedListener);
            mAirSpeedMiddleLayout.setOnClickListener(mSpeedListener);
            mAirSpeedFastLayout.setOnClickListener(mSpeedListener);

        }

        if (mAirSpeedPop == null) {

            mAirSpeedPop = new PopupWindow(mAirSpeedPopView, mAirSpeedLayout.getWidth(), 424);
            // 设置动画
            //            mAirSpeedPop.setAnimationStyle(R.style.take_photo_anim);
            //  设置背景颜色
            mAirSpeedPop.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
            // 设置可以获取焦点
            mAirSpeedPop.setFocusable(true);
            // 设置可以触摸弹出框以外的区域
            mAirSpeedPop.setOutsideTouchable(true);

        }

        if (mAirSpeedPop.isShowing()) {
            mAirSpeedPop.dismiss();
        } else {
            // 以下拉的方式显示，并且可以设置显示的位置
            mAirSpeedPop.showAsDropDown(mAirBtnListLayout, mAirModeLayout.getWidth(), 0);
        }

    }

    private String getIrCode() {
        int[] patternsInArray = mKKACManager.getACIRPatternIntArray();//这些码可以直接给ConsumerIR发送出去
        Log.e("IRPattern", Arrays.toString(patternsInArray));
        if (patternsInArray != null && patternsInArray.length > 0) {
            String code = "";
            int length = patternsInArray.length;
            for (int i = 0; i < length; i++) {
                if (i < length - 1) {
                    code += patternsInArray[i] + ",";
                } else {
                    code += patternsInArray[i];
                }
            }
            return code;
        }
        return null;
    }

    private void sendIr() {
        String irCode = BlirConst.kk2bl(getIrCode());
        if (irCode != null) {
            if (mCheckable) {
                int mode = mKKACManager.getCurModelType();
                int power = mKKACManager.getPowerState();
                int tmp = mKKACManager.getCurTemp();
                int speed = mKKACManager.getCurWindSpeed();
                int dir = mKKACManager.getCurUDDirect();

                Log.e("Code", "mode: " + mode);
                Log.e("Code", "power: " + power);
                Log.e("Code", "tmp: " + tmp);
                Log.e("Code", "speed: " + speed);
                Log.e("Code", "dir: " + dir);
                Log.e("Code", "code: " + irCode);
                String blirCode = intToHex(mode) + intToHex(power) + intToHex(tmp) +
                        intToHex(speed) + intToHex(dir) + irCode;
                Log.e("Code", "blirCode: " + blirCode);
                CodeviewBean acCodeBean = new CodeviewBean(mSubBLir.getDbId(), (int) mSubBLir.getSubId()
                        , mSubBLir.getDevType(), DBUtils.CODE_TYPE_STATUS, DBUtils.CODE_TYPE_STATUS,
                        DBUtils.CODE_TYPE_STATUS, blirCode, 0);
                mSubBLir.setBatchCodes(acCodeBean);
            }
            new SendIrTask(getContext(), mSubBLir, mOnlineChangeListener, mOfflineMask).execute(irCode);
            String acStateV2InString = mKKACManager.getACStateV2InString();
            DataStoreUtil.i().putString(mSubBLir.getUDID(), acStateV2InString);
        }
    }

    private String intToHex(int aValue) {
        String tHex = Integer.toHexString(aValue);

        if (tHex.length() < 8) {
            char tZero[] = new char[8 - tHex.length()];

            Arrays.fill(tZero, '0');
            return new String(tZero) + tHex;
        } else {
            return tHex;
        }
    }

}
